//
//  KXUIButton.m
//  KXFrd
//
//  Created by liu feng on 12-2-6.
//  Copyright (c) 2012年 kaixin001. All rights reserved.
//

#import "KXUIButton.h"

@implementation KXUIButton

@synthesize imageAnimationType;

-(void) dealloc
{
    [self cancelCurrentImageLoad];
    [super dealloc];
}

- (void)setImageWithURL:(NSString *)url;
{
    [self setImageWithURL:url 
         placeholderImage:nil];
}

- (void)setImageWithURL:(NSString *)url 
       placeholderImage:(UIImage *)placeholder
{
    showAnimation = NO;
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    
    // Remove in progress downloader from queue
    [manager cancelForDelegate:self];
    
    [self setBackgroundImage:placeholder forState:UIControlStateNormal];
//    [self setImage:placeholder forState:UIControlStateNormal];
    [KXImageAnimation imageShowAnimation:self withOldImg:placeholder withType:KXIMAGE_ANIMATION_FLIP_LEFT];
    if (url)
    {
        [manager downloadWithURL:[NSURL URLWithString:url] delegate:self];
    }
}

- (void)cancelCurrentImageLoad
{
    [[SDWebImageManager sharedManager] cancelForDelegate:self];
}

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
    UIImage* oldImg = [[[self backgroundImageForState:UIControlStateNormal] retain] autorelease];
    
    [self setBackgroundImage:image forState:UIControlStateNormal];
//    [self setImage:image forState:UIControlStateNormal];
    
    if (showAnimation && imageAnimationType != KXIMAGE_ANIMATION_NONE)
    {
        [KXImageAnimation imageShowAnimation:self withOldImg:oldImg withType:self.imageAnimationType];
    }
}

- (void)webImageManager:(SDWebImageManager *)imageManager didRecieveImageData:(NSNumber *)dataSize
{
    showAnimation = YES;
}

@end
